package month1;

public class MaxPathSum124_05 {
    public static void main(String[] args) {
        System.out.println(maxPathSum(new TreeNode(new Integer[]{1, 2, 3})));
    }

    /**
     * ret表示结果
     */
    static int ret;
    static int maxPathSum(TreeNode root) {
        ret = Integer.MIN_VALUE;
        dfs(root);
        return ret;
    }

    /**
     * 递归
     * @param root
     * @return 这里返回的寓意以根结点为值的左右子树的较大值
     */
    static int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = Math.max(dfs(root.left), 0), right = Math.max(dfs(root.right), 0);
        ret = Math.max(ret, left + right + root.val);
        return Math.max(left, right) + root.val;
    }
}
